package com.feiyi.app;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.blankj.utilcode.util.BarUtils;
import com.feiyi.app.base.BaseActivity;

import com.feiyi.app.fragment.CartFragment;
import com.feiyi.app.fragment.ClassificationFragment;
import com.feiyi.app.fragment.HomePageFragment;
import com.feiyi.app.fragment.MineFragment;
import com.feiyi.app.utils.FitStateUI;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者 : wlt
 * 创建时间:  2018-07-12
 * QQ : 1346947404
 * 邮箱:wanglt_android@163.com
 * 类说明:
 * In me the tiger sniffs the rose
 */

public class MainActivity extends BaseActivity {
    /**
     * 底部切换按钮的容器
     */
    public static RadioGroup radiogroup_main_foot;
    /**
     * fragment管理者
     */
    private FragmentManager fm;
    /**
     * 退出时间
     */
    private long mExitTime;
    /**
     * fragment的动作集合对象
     */
    private FragmentTransaction ft;

    /**
     * 当前app是否在运行,并且是没有进入详情的情况下
     * 这个主要用于接推送的时候进行的判断
     */
    public static Boolean isForeground = false;

    /**
     * fragment 的集合,用于放四个主界面的
     */
    private List<Fragment> fragments = new ArrayList<>();

    /**
     * 用户id
     */
    private String user;
    /**
     * log标签
     */
    private static final String TAG = "MainActivity";


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        FitStateUI.setImmersionStateMode(this);
        setContentView(R.layout.activity_main);

        fm = getSupportFragmentManager();
        initFragment();
        initView();
//        EventBus.getDefault().register(this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.e(TAG, "onNewIntent: " );
        if (null != intent.getStringExtra("shop_cart")){
            ((RadioButton)findViewById(R.id.radio_main_foot_cart)).performClick();
//            changeCart();
        }
    }

    private void initFragment() {
        HomePageFragment homePageFragment = new HomePageFragment();
        fragments.add(homePageFragment);

        ClassificationFragment classificationFragment = new ClassificationFragment();
        fragments.add(classificationFragment);

        CartFragment cartFragment = new CartFragment();
        fragments.add(cartFragment);

        MineFragment mineFragment = new MineFragment();
        fragments.add(mineFragment);
    }

    private void initView() {
        radiogroup_main_foot = (RadioGroup)findViewById(R.id.radiogroup_main_foot);

        radiogroup_main_foot.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.radio_main_foot_home:
                        changeHome();
                        break;
                    case R.id.radio_main_foot_classify:
                        changeClassify();
                        break;

                    case R.id.radio_main_foot_cart:
                        changeCart();
                        break;
                    case R.id.radio_main_foot_mine:
                        changeMine();
                        break;
                }
            }
        });
        changeHome();
    }
    /**
     * 切换到个人中心
     */
    private void changeMine() {
        ft = fm.beginTransaction();
        if (!fragments.get(3).isAdded()) {
            ft.add(R.id.frame_container, fragments.get(3));
        }
        ft.hide(fragments.get(0));
        ft.hide(fragments.get(1));
        ft.hide(fragments.get(2));
        ft.show(fragments.get(3));
        ft.commit();
    }

    /**
     * 切换到购物车
     */
    private void changeCart() {
        ft = fm.beginTransaction();
        if (!fragments.get(2).isAdded()) {
            ft.add(R.id.frame_container, fragments.get(2));
        }
        ft.hide(fragments.get(3));
        ft.hide(fragments.get(1));
        ft.hide(fragments.get(0));
        ft.show(fragments.get(2));
        ft.commit();
    }

    /**
     * 切换到分类
     */
    public void changeClassify() {
        ft = fm.beginTransaction();
        if (!fragments.get(1).isAdded()) {
            ft.add(R.id.frame_container, fragments.get(1));
        }
        ft.hide(fragments.get(3));
        ft.hide(fragments.get(0));
        ft.hide(fragments.get(2));
        ft.show(fragments.get(1));
        ft.commit();
    }

    /**
     * 切换到首页
     */
    private void changeHome() {
        ft = fm.beginTransaction();
        if (!fragments.get(0).isAdded()) {
            ft.add(R.id.frame_container, fragments.get(0));
        }
        ft.hide(fragments.get(3));
        ft.hide(fragments.get(1));
        ft.hide(fragments.get(2));
        ft.show(fragments.get(0));
        ft.commit();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        EventBus.getDefault().unregister(this);
    }
}
